<template>
  <div class="export">
    <el-date-picker
      v-model="dataValue"
      type="datetimerange"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="selectTime"
    >
    </el-date-picker>
    <el-button type="primary" @click="exportAction"> 导出</el-button>
  </div>
</template>

<script>
export default {
    props: {
    number: {
      //物联网设备编号
      type: String,
      default: "1",
    },
  },
  data() {
    return {
      dataValue: "",
      startTime: "",
      endTime: "",
    };
  },
  methods: {
    changeTimeFormat(str) {
      this.clock = (str + "").split(" ")[4];
      if ((str + "").indexOf("-") != -1) {
        str = str.replace(new RegExp(/-/gm), "/");
      }
      let d = new Date(str);
      let newDateYear = d.getFullYear();
      let newDateMonth =
        d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
      let newDateDay =
        d.getDate() + "" < 10 ? "0" + d.getDate() + "" : d.getDate() + "";
      return (
        newDateYear + "-" + newDateMonth + "-" + newDateDay + " " + this.clock
      );
    },
    selectTime(val) {
      this.startTime = this.changeTimeFormat(val[0]);
      this.endTime = this.changeTimeFormat(val[1]);
    },
    exportAction() {
      this.$get("/uploadFile/exportWaterColumnBoxDataLog", {
        number: this.number,
        startTime: this.startTime,
        endTime: this.endTime,
      }).then((res) => {
        if(res.code==0){
            this.$message(res.msg)
            window.open(res.data.filePath)
        }else{
            this.$message(res.msg)
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.export{
    display: flex;
    justify-content: space-around;
}
</style>